package Middlewares 
import (
	"github.com/gin-gonic/gin"
	"net/http"
	"strings"
)
func init(){

	
}
// 写一个中间件，统计视图函数运行时间
func TotalTime(c *gin.Context)  {
	// 解决跨域问题
	c.Header("Access-Control-Allow-Origin", "*")
    c.Header("Access-Control-Allow-Headers", "Content-Type,AccessToken,X-CSRF-Token, Authorization, Token")
    c.Header("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, PATCH, DELETE")
    c.Header("Access-Control-Expose-Headers", "Content-Length, Access-Control-Allow-Origin, Access-Control-Allow-Headers, Content-Type")
    c.Header("Access-Control-Allow-Credentials", "true")
    //这里是解决vue那个坑
    method := c.Request.Method
	c.Set("status", 200)
    if method == "OPTIONS" {
        c.AbortWithStatus(http.StatusNoContent)
    }
	if c.FullPath() == "/User/login" {
		c.Next() 
	}
	// 判断请求头中是否有token
	aut := c.Request.Header.Get("Authorization")
	if ok := strings.HasPrefix(aut, "Bearer "); aut == "" || !ok {
		c.Set("status", 401)
	    c.Set("islogin", false)
		c.Next() 
	}
	c.Next() 
    

}
